package com.homework.controller;

import com.homework.dto.HomeworkCreateDTO;
import com.homework.dto.Result;
import com.homework.entity.Homework;
import com.homework.entity.HomeworkDetail;
import com.homework.mapper.HomeworkDetailMapper;
import com.homework.service.HomeworkService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/homework")
@RequiredArgsConstructor
public class HomeworkController {

    private final HomeworkService homeworkService;
    private final HomeworkDetailMapper homeworkDetailMapper;

    /**
     * 创建作业
     */
    @PostMapping
    public Result<String> createHomework(@Valid @RequestBody HomeworkCreateDTO dto) {
        return homeworkService.createHomework(dto);
    }

    /**
     * 更新作业
     */
    @PutMapping("/{id}")
    public Result<String> updateHomework(@PathVariable Long id, @Valid @RequestBody HomeworkCreateDTO dto) {
        dto.setId(id);
        return homeworkService.updateHomework(dto);
    }

    /**
     * 删除作业
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteHomework(@PathVariable Long id) {
        return homeworkService.deleteHomework(id);
    }

    /**
     * 获取当前作业（最早的）
     */
    @GetMapping("/current")
    public Result<Homework> getCurrentHomework(@RequestParam Long studentId) {
        return homeworkService.getCurrentHomework(studentId);
    }

    /**
     * 获取所有当前作业
     */
    @GetMapping("/current/all")
    public Result<java.util.List<Homework>> getAllCurrentHomework(@RequestParam Long studentId) {
        return homeworkService.getAllCurrentHomework(studentId);
    }

    /**
     * 开始作业
     */
    @PostMapping("/{id}/start")
    public Result<String> startHomework(@PathVariable Long id) {
        return homeworkService.startHomework(id);
    }

    /**
     * 完成作业项
     */
    @PostMapping("/detail/{detailId}/complete")
    public Result<String> completeHomeworkDetail(@PathVariable Long detailId) {
        return homeworkService.completeHomeworkDetail(detailId);
    }

    /**
     * 获取作业明细列表
     */
    @GetMapping("/{id}/details")
    public Result<List<HomeworkDetail>> getHomeworkDetails(@PathVariable Long id) {
        List<HomeworkDetail> details = homeworkDetailMapper.selectList(
            new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<HomeworkDetail>()
                .eq(HomeworkDetail::getHomeworkId, id)
                .orderByAsc(HomeworkDetail::getSequenceOrder)
        );
        return Result.success(details);
    }

    /**
     * 动态添加作业项
     */
    @PostMapping("/{id}/detail")
    public Result<String> addHomeworkDetail(@PathVariable Long id, @RequestBody com.homework.dto.HomeworkDetailDTO dto) {
        return homeworkService.addHomeworkDetail(id, dto);
    }

    /**
     * 删除未完成作业项
     */
    @DeleteMapping("/{id}/detail/{detailId}")
    public Result<String> deleteHomeworkDetail(@PathVariable Long id, @PathVariable Long detailId) {
        return homeworkService.deleteHomeworkDetail(id, detailId);
    }

    /**
     * 修改作业项内容
     */
    @PutMapping("/{id}/detail/{detailId}")
    public Result<String> updateHomeworkDetail(@PathVariable Long id, @PathVariable Long detailId, @RequestBody com.homework.dto.HomeworkDetailDTO dto) {
        return homeworkService.updateHomeworkDetail(id, detailId, dto);
    }

    /**
     * 调整作业项必须/非必须属性
     */
    @PutMapping("/{id}/detail/{detailId}/required")
    public Result<String> updateHomeworkDetailRequired(@PathVariable Long id, @PathVariable Long detailId, @RequestBody java.util.Map<String, Boolean> request) {
        return homeworkService.updateHomeworkDetailRequired(id, detailId, request.get("isRequired"));
    }
}